package com.takanafu.haru.exp{
	import com.takanafu.haru.code.CodeBase;
	import com.takanafu.haru.code.CodeBoolean;
	import com.takanafu.haru.core.CharacterType;
	import com.takanafu.haru.core.TokenType;
	
	public class BinaryExpression extends CodeBase{
		private var operation:int;
		protected var left:CodeBase;
		protected var right:CodeBase;
		
		public function BinaryExpression(operation:int,left:CodeBase,right:CodeBase){
			this.operation = operation;
			this.left = left;
			this.right = right;
		}
		
		override public function run():CodeBase{
			var l:CodeBase = this.left.run();
			var r:CodeBase = this.right.run();
			var result:CodeBase = null;
			
			switch(this.operation){
				case CharacterType.PLUS:
					result = l.add(r);
					break;
				case CharacterType.MINUS:
					result = l.sub(r);
					break;
				case CharacterType.MUL:
					result = l.mul(r);
					break;
				case CharacterType.DEV:
					result = l.dev(r);
					break;
				case CharacterType.LES:
					result = l.les(r);
					break;
				case CharacterType.GRE:
					result = l.gre(r);
					break;
				case TokenType.LE:
					result = l.le(r);
					break;
				case TokenType.GE:
					result = l.ge(r);
					break;
				case TokenType.EQ:
					if(l.toString() == r.toString()){
						result = CodeBoolean.True;
					}else{
						result = CodeBoolean.False;
					}
					break;
				case TokenType.NE:
					if(l.toString() != r.toString()){
						result = CodeBoolean.False;
					}else{
						result = CodeBoolean.True;
					}
					break;
			}
			return result;
		}
	}
}